/**
 * -- 浏览器类
 * 用于获取浏览器相关信息
 */

const navigator = window.navigator ? window.navigator : false;
let userAgent = '';
if (!navigator) {
	console.error('[llm-web-tool browser] window.navigator is undefined')
} else {
	userAgent = navigator.userAgent;
}


const isPC = (): boolean => !(userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))

const isMobile = (): boolean => (userAgent.match(/(phone|pod|iPhone|iPod|ios|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) && !isPad()

const isPad = (): boolean => {
	let isAndroid = /(?:Android)/.test(userAgent);
	let isFireFox = /(?:Firefox)/.test(userAgent);
	return /(?:iPad|PlayBook)/.test(userAgent) || (isAndroid && !/(?:Mobile)/.test(userAgent)) || (isFireFox && /(?:Tablet)/.test(userAgent))
}

const isSafari = (): boolean => /Safari/.test(userAgent) && !/Chrome/.test(userAgent);

const isWechat = (): boolean => !!(userAgent.match(/MicroMessenger/i));

const isIE = (): boolean => userAgent.indexOf('Trident') > -1;

const isOpera = (): boolean => userAgent.indexOf('Presto') > -1;

const isWebkit = (): boolean => userAgent.indexOf('AppleWebKit') > -1;

const isAndroid = (): boolean => userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1;

const isIos = (): boolean => !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

const info = () => ({
	isPC: isPC(),
	isMobile: isMobile(),
	isPad: isPad(),
	isSafari: isSafari(),
	isWechat: isWechat(),
	isIE: isIE(),
	isOpera: isOpera(),
	isWebkit: isWebkit(),
	isAndroid: isAndroid(),
	isIos: isIos(),
})

export { info, isPC, isMobile, isPad, isSafari, isWechat, isIE, isOpera, isWebkit, isAndroid, isIos }